package com.ebt.m.customer.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ebt.m.R;
import com.ebt.m.commons.buscomponent.listview.IView;
import com.ebt.m.commons.utils.NetUtils;
import com.ebt.m.customer.CustomerUtil;
import com.ebt.m.customer.constant.CustomerConstant;
import com.ebt.m.customer.entity.CustomerSimple;
import com.ebt.m.customer.event.EventACustomerChoosen;
import com.ebt.m.customer.ui.ActivityCustomerAll;
import com.jakewharton.rxbinding2.view.RxView;

import org.greenrobot.eventbus.EventBus;

import java.util.concurrent.TimeUnit;

/**
 * 保单客户itemview
 *
 * @author Damon.Han
 */
public class CustomerItemPolicy extends IView {

    private TextView mNameView;
    private ImageView mAvatarView;
    private CustomerSimple mCustomerActive;

    public void setFrom(int from) {
        this.from = from;
    }

    private int from;

    public CustomerItemPolicy(Context context) {
        this(context, null);
    }

    @Override
    public void update(Object... args) {
        mCustomerActive = (CustomerSimple) args[0];
        if (mCustomerActive == null) {
            return;
        }
        mNameView.setText(mCustomerActive.getName());
        CustomerUtil.setAvatarByUrlAndSex(getContext(), mAvatarView, mCustomerActive.portraitPath, mCustomerActive.sex);
    }

    public CustomerItemPolicy(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomerItemPolicy(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View contentView = inflate(context, R.layout.view_customer_item_policy, this);
        mNameView = (TextView) contentView.findViewById(R.id.tv_name);
        mAvatarView = (ImageView) contentView.findViewById(R.id.iv_avatar);
        RxView.clicks(contentView.findViewById(R.id.rl_item_view))
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(aVoid -> {
                    if (NetUtils.isNetworkAvailable(getContext().getApplicationContext())) {
                        if (from == CustomerUtil.FROM_OTHER) {
                            Intent intent = new Intent(getContext(), ActivityCustomerAll.class);
                            intent.putExtra(CustomerConstant.CUSTOMER_DETAIL_MODE, CustomerConstant.CUSTOMER_DETAIL_MODE_EDIT);
                            intent.putExtra(CustomerConstant.CUSTOMER_UUID, mCustomerActive.getCustomerId());
                            intent.putExtra(CustomerConstant.CustomerAll.VIEW_PAGER_INDEX, CustomerConstant.CustomerAll.VIEW_PAGER_INDEX_PROFILE);
                            ActivityCompat.startActivity(getContext(), intent, ActivityOptionsCompat.makeSceneTransitionAnimation((Activity) getContext(), mAvatarView, "translate_view").toBundle());
                        } else if (from == CustomerUtil.FROM_CHOOSE) {
                            EventACustomerChoosen eventACustomerChoosen = new EventACustomerChoosen();
                            eventACustomerChoosen.customerActive = mCustomerActive;
                            EventBus.getDefault().post(eventACustomerChoosen);
                            ((Activity) getContext()).finish();
                        }
                    } else {
                        Toast.makeText(getContext().getApplicationContext(), getContext().getString(R.string.network_fail), Toast.LENGTH_SHORT).show();
                    }
                });
    }

}